package com.cxy.instarsystem.web;

import com.cxy.instarsystem.po.VCategory;
import com.cxy.instarsystem.po.Video;
import com.cxy.instarsystem.service.VCategoryService;
import com.cxy.instarsystem.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VCategoryService vCategoryService;

    @GetMapping("video")
    public String videoPage(@PageableDefault(size = 18, sort = {"createTime"}, direction = Sort.Direction.DESC)
                                        Pageable pageable,Model model){

        //从数据库拿到视频的相关信息
    model.addAttribute("videos",videoService.listVideos(pageable,null,"0"));
    model.addAttribute("total",videoService.getTotalNum());
//    model.addAttribute("vcategories",videoService.groupCounts(vCategoryService.listCategory()));
        model.addAttribute("vcategories",vCategoryService.listCategory());
        return "video";
    }

    @PostMapping("videos")
    public String changePage(@PageableDefault(size = 18, sort = {"createTime"}, direction = Sort.Direction.DESC)
                                    Pageable pageable,Model model){

        //从数据库拿到视频的相关信息
        model.addAttribute("videos",videoService.listVideos(pageable,null,"0"));

        return "video :: videolist";
    }

    @PostMapping("videolist")
    public String getVideos(@PageableDefault(size = 18, sort = {"createTime"}, direction = Sort.Direction.DESC)
                                        Pageable pageable, @RequestParam String value, Model model){

        model.addAttribute("videos",videoService.listVideos(pageable,null,value));

        return "video :: videolist";
    }

    @PostMapping("video/search")
    public String searchVideo(@PageableDefault(size = 18, sort = {"createTime"}, direction = Sort.Direction.DESC)
                                          Pageable pageable,String condition,String id, Model model){
        model.addAttribute("videos",videoService.listVideos(pageable,condition,id));
        return "video :: videolist";
    }

}
